package com.qf.co.Dao;

import com.qf.co.pojo.Profile;
import com.qf.co.utils.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author hanjiejie
 * @version V1.0
 * @Project java2203
 * @Package com.qf.co.Dao
 * @Description:
 * @Date 2022/5/28 22:11
 */
public class ProfileDaoImpl implements ProfileDao{
    //创建Dbutil工具
    private QueryRunner queryRunner =  new QueryRunner(DBUtil.getDateSource());
    @Override
    public List<Profile> findAll() {
        String sql ="SELECT *FROM t_profile  ";
        try {
            List<Profile> list = queryRunner.query(sql, new BeanListHandler<Profile>(Profile.class));
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Profile allone(String id) {
        String sql ="SELECT *FROM t_profile WHERE id=?";
        try {
            Profile query = queryRunner.query(sql, new BeanHandler<Profile>(Profile.class), id);
            return query;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int update(Profile profile) {
        String sql ="update t_profile set name=?, birthday=?,gender=?," +
                "career=?,address=?,mobile=?,prcture=? where id=?";
        try {
            int count = queryRunner.update(sql, profile.getName(), profile.getBirthday(), profile.getGender(),
                    profile.getCareer(), profile.getAddress(), profile.getMobile(), profile.getPicture(), profile.getId());
       return count;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
